<?php
// No directAcces
defined('_AppBackMiage') or die('Restricted access');

include_once (DS . "backoffice/include/header.php");

if(isset($_GET['uid']) && !empty($_GET['uid']))
{
    $date = new Date($_GET["uid"]);
}
else
{
    // Rediredction
}

if(isset($_POST['submit']) && isset($_GET["uid"]) && !empty($_GET["uid"]) && $_GET['uid'] == $_POST['id'])
{

    $form = true;
    if (isset($_POST['titre']) && !empty($_POST['titre']))
    {
        $tmp = $date->setTitreDate($_POST['titre']);
        if ($tmp['boolOk'] === false)
        {
            $errMSG = $tmp['errmsg'];
            $form = false;
        }
    }
    if (isset($_POST['valeur']) && !empty($_POST['valeur']))
    {
        switch ($_POST['type'])
        {
            case 0:
                $tmp = $date->setValeurDate(Tools::transformDateToTimestamp($_POST['valeur']));
                break;
            case 1:
                $mois = 0;
                $jours = 0;
                if( isset($_POST['valeur']) && !empty($_POST['valeur']))
                {
                    foreach($_POST['valeur'] as $info => $valeur)
                    {
                        if($info == "mois")
                        {
                            $mois = intval($valeur);
                        }
                        if( $info == "jours" )
                        {
                            $jours = intval($valeur);
                        }
                    }
                }
                $time = Tools::transformDureeToTimestamp($mois, $jours);
                $tmp = $date->setValeurDate($time);
                break;
            case 2:
                $tmp = $date->setValeurDate($_POST['valeur']);
                break;
        }
        if ($tmp['boolOk'] === false)
        {
            $errMSG = $tmp['errmsg'];
            $form = false;
        }
    }

    if($form)
    {
        $date->update();
    }

}

?>
<div id="wrapper">
    <div id="content">
        <div id="box">
            <h3 id="adduser">&Eacute;dition d'une date</h3>
            <form id="form" method="post">
            <fieldset id="personal">
                <legend>Informations</legend>
                <label>Promo : </label>
                <?php $promo = new Promo($date->getIdPromo()); echo stripcslashes($promo->getNom()); ?>
                <br/>
                <label for="titre">Titre :</label>
                <input type="text" name="titre" value="<?php echo stripcslashes($date->getTitreDate()); ?>"/>
                <br/>
                <label for="valeur">Valeur : </label>
                <?php
                    switch($date->getTypeDate())
                    {
                        case 0:
                            ?>
                <input name="valeur" class="datepicker" id="valeur" type="text" tabindex="1" value="<?php echo date('d/m/Y',$date->getValeurDate()); ?>"/>
                            <?php
                            break;
                        case 1:
                            $time = Tools::transformTimestampToDuree($date->getValeurDate());
                            $mois = $time['mois'];
                            $jours = $time['jours'];
                            ?>
                            <input name="valeur[mois]" type="text" size="1" value="<?php echo $mois; ?>"/> mois et <input name="valeur[jours]" type="text" size="2" value="<?php echo $jours; ?>"/> jours
                            <br/>
                            <?php
                            break;
                        case 2:
                            ?>
                            <input name="valeur" id="valeur" type="text" tabindex="1" value="<?php echo stripcslashes($date->getValeurDate()); ?>"/>
                            <?php
                            break;
                    }
                ?>
                <?php if($form===false){ echo $errMSG; } ?>
                <br />
                <input type="hidden" name="id" value="<?php echo $date->getIdDate(); ?>"/>
                <input type="hidden" name="type" value="<?php echo $date->getTypeDate(); ?>"/>
            </fieldset>
            <div align="center">
                <input id="button1" type="submit" name="submit" value="Sauver" />
            </div>
            </form>
        </div>
    </div>
<?php
    include_once (DS . "backoffice/include/sidebar.php");
    include_once (DS . "backoffice/include/footer.php");
?>

